package com.sevenGroup.pawnshop.service.impl;

import com.sevenGroup.pawnshop.Utils.CopyProperties;
import com.sevenGroup.pawnshop.Utils.TimeUtils;
import com.sevenGroup.pawnshop.controller.form.AuthenticateForm;
import com.sevenGroup.pawnshop.controller.form.AuthenticateSelectConditionalForm;
import com.sevenGroup.pawnshop.controller.info.AuthenticateInfo;
import com.sevenGroup.pawnshop.dao.GoodsDao;
import com.sevenGroup.pawnshop.model.Authenticate;
import com.sevenGroup.pawnshop.dao.AuthenticateDao;
import com.sevenGroup.pawnshop.model.AuthenticateSelectConditional;
import com.sevenGroup.pawnshop.service.AuthenticateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@Service
public class AuthenticateServiceImpl extends ServiceImpl<AuthenticateDao, Authenticate> implements AuthenticateService {
    @Resource
    private AuthenticateDao authenticateDao;
    @Resource
    private GoodsDao goodsDao;

//  新增鉴定
    @Override
    @Transactional
    public Integer addAuthenticate(AuthenticateForm authenticateForm) {
//        先转换并设置时间
        Authenticate authenticate = CopyProperties.copyOne(authenticateForm, Authenticate.class);
        authenticate.setAuthenticateTime(TimeUtils.getNowTimeString());
//        调用dao更新鉴定表
        Integer num=authenticateDao.addAuthenticate(authenticate);
//        更新商品表，先查询该商品是否为待鉴定状态，如果为，则改为待评估
        if ("待鉴定".equals(goodsDao.selectById(authenticate.getGoodId()).getGoodState())){
            goodsDao.updateGoodState(authenticate.getGoodId(),"待评估");
        }
        return num;
    }
//  查询鉴定集合
    @Override
    public List<AuthenticateInfo> selectAuthenticate(AuthenticateSelectConditionalForm conditionalForm) {
//        先转换并将属性中时间集合拆分成开始时间与结束时间
        AuthenticateSelectConditional conditional = CopyProperties.copyOne(conditionalForm, AuthenticateSelectConditional.class);
//        设置查询时间时先判断是否为空
        if (null==conditionalForm.getAuthenticateTimes()){
//            如果为空，不设置
        }else{
//            如果不为空，设置开始时间与结束时间
            conditional.setAuthenticateBeginTimes(conditionalForm.getAuthenticateTimes().get(0));
            conditional.setAuthenticateEndTimes(conditionalForm.getAuthenticateTimes().get(1));
        }
//        调用dao查询
        List<Authenticate> authenticates=authenticateDao.selectAuthenticate(conditional);
        return CopyProperties.copyList(authenticates,AuthenticateInfo.class);
    }
//  删除鉴定
    @Override
    public Integer deleteAuthenticateById(Integer id) {
        return null;
    }
}
